package main
import (
	"fmt"
	"unsafe"
)



func main() {
	var (
		array []int = []int{7,4,2,8,1,9,0,3,6,5,}
	)

	求和()

	fmt.Println(" >>>>>>>>(array) ",(array))

	//fmt.Println(" >>>>>>>>快速(array) ",快速(array))

	//fmt.Println(" >>>>>>>>冒泡(array) ",冒泡(array))

	//fmt.Println(" >>>>>>>>插入(array) ",冒泡(array))
	list := 希尔(array)
	fmt.Println(" >>>>>>>>希尔(array) ",list)

	fmt.Println(" >>>>>>>>&array ",&(array))
	fmt.Println(" >>>>>>>>&list ",&list)

	fmt.Printf("unsafe addr %p, align %d, size %d\n", &array, unsafe.Alignof(array), unsafe.Sizeof(array))
	fmt.Printf("unsafe addr %p, align %d, size %d\n", &list, unsafe.Alignof(list), unsafe.Sizeof(list))


	var a int8 = 1 // 声明一个变量
	fmt.Println(a) 
	// 先将a的指针转为Pointer类型，再将Pointer类型转为*int8指针
    i := (*int8)(unsafe.Pointer(&a))
    
	*i = 100 // 对转换来的新指针变量进行赋值操作，改变a的值
	fmt.Println(a)
	
	fmt.Println(" end ")


}

// 1,2,3 ...N
func 求和()  {
	var (
		i int = 0 
		s int = 0
		l int = 1000
	)

	s = (i + l) * l / 2
	fmt.Println(" >>>>>>>> ",s)
}

func 平方阶()  {
	var (
		i int 
		j int
		n int
	)	
	for i = 0; i < n; i++ {
		for j = 0; j < n; j++ {
			// TODO  O(n*n)
		}
	}
}

func 冒泡(arg []int) []int {
	
	if len(arg) == 0 || len(arg) == 1 { return arg }
	
	fmt.Println(" >>>>>>>> 冒泡 arg:",arg)
	var (
		count 	int
		count2 	int
		flag	bool = true
	) 
	
	for i := 0; i < len(arg) && flag ; i++ {
		flag = false
		for j := 0; j < len(arg); j++ {
			temp := arg[i]
			if  arg[i] < arg[j] {
				arg[i] = arg[j]
				arg[j] = temp

				flag = true
				count2++
			}
			count++
		}
	}
	
	// fmt.Println(" >>>>>>>> count:",count)
	// fmt.Println(" >>>>>>>> count2:",count2)
	return arg
}


func 快速(arg []int) []int {
	if len(arg) == 0 || len(arg) == 1 { return arg }
	
	fmt.Println(" >>>>>>>> 快速 arg:",arg)

	mid := arg[0]
	var left 	[]int
	var right 	[]int

	for _ , temp := range arg[1:] {
		if temp < mid {
			left = append(left,temp)
		}else{
			right = append(right,temp)
		}
	}

	// fmt.Println(" >>>>>>>> left:",left)
	// fmt.Println(" >>>>>>>> right:",right)

	left 	= 快速(left)
	right 	= 快速(right)

	list 	:= append(left,mid)

	list 	= append(list,right...)

	return list
}


func 插入(arg []int) []int {
	if len(arg) == 0 || len(arg) == 1 { return arg }

	fmt.Println(" >>>>>>>> 插入 arg:",arg)

	for i := 0; i < len(arg); i++ {
		temp := arg[i]
		
		for j := i - 1 ; j > 0 && arg[j] > temp; i-- {
			arg[j+1] = arg[j]
		}
	}
	
	return arg
}


func 希尔(arg []int) []int {
	var (
		d int = len(arg) / 2
	)

	for	d > 0 {
		for i:=0;i<d;i++ {
			for j := i+d;j<len(arg);j+=d {
				temp := arg[j]
				k := j-d
				for k>=i && arg[k] > temp {
					arg[k+d] = arg[k]
					k -= d
				}
				arg[k+d]= temp
			}
		}
		
		fmt.Println(" >>>>>>>>插入(d) ",d)
		d = d / 2 
	}
	return arg
}
